package com.siti.kindnessplatform.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import net.sf.json.JSONObject;

public class AddressLngLatExchange {
	public static String getLngLat(String address) {
        StringBuffer json = new StringBuffer();
        try {
            URL u = new URL("http://restapi.amap.com/v3/geocode/geo?address="+address+"&output=JSON&key=7f4ffae4074e8b8e4d147190527a4b72");
            URLConnection yc = u.openConnection();
            //读取返回的数据
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"UTF-8"));
            String inputline = null;
            while((inputline=in.readLine())!=null){
                json.append(inputline);
            }
        in.close();
        } catch (Exception e) {
             e.printStackTrace();
         }
        String jsonStr=json.toString();
        //System.out.println("jsonStr:"+jsonStr);
        if(jsonStr.length()!=0){
        	JSONObject jsonObject = JSONObject.fromObject(jsonStr);  
        	//判断输入的位置点是否存在
            if(jsonObject.getJSONArray("geocodes").size()>0)
                return jsonObject.getJSONArray("geocodes").getJSONObject(0).get("location").toString();
            else
                return null;
        }else{
        	return null;
        } 
    }
    public static void main(String[] args)  {
        AddressLngLatExchange addressLngLatExchange=new AddressLngLatExchange();
        System.out.println(addressLngLatExchange.getLngLat("湖北省武汉市武昌区紫阳路99号"));
        
    }
}
